package com.wenwu.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsNaturalEnvironment;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.dao.RelicsAttachmentMapper;
import com.wenwu.dao.RelicsNaturalEnvironmentMapper;
import com.wenwu.service.NaturalService;

/**
 * @project RelicsProject-service
 * @class NaturalServiceImpl.java
 * @author zhujiagui E-mail:<zhujiagui@zkingsoft.com>
 * @date 2015年5月13日 下午6:47:15
 * @description 自然环境
 */
@Service
public class NaturalServiceImpl implements NaturalService {
	@Autowired
	private RelicsNaturalEnvironmentMapper naturalMapper;
	@Autowired
	private RelicsAttachmentMapper attachmentMapper;
	public String addObj(RelicsNaturalEnvironment natural)
			throws RelicsException {
		int result = 0;
		try {
			List<RelicsAttachment> list = natural.getFileList();
			if (natural.getNaturalId()==null) {
				result += naturalMapper.insertSelective(natural);
				if (natural.getNaturalId() > 0) {
					if (list != null && list.size() > 0) {
						for (RelicsAttachment attachment : list) {
							attachment.setAttachmentDesc("自然环境附件");
							attachment.setTableOwner(natural.getNaturalId());// 外键
						}
						result += attachmentMapper.insertList(list);
					}
				}
				if (result > 0) {
					return "自然环境添加成功";
				} else {
					return "自然环境添加失败";
				}
			} else {
				result += attachmentMapper
						.deleteByKey(natural.getNaturalId(),natural.getOwnList());// 先删除原有的附件，再添加
				if (list != null && list.size() > 0) {
					for (RelicsAttachment attachment : list) {
						attachment.setAttachmentDesc("自然环境附件");
						attachment.setTableOwner(natural.getNaturalId());// 外键
					}
					result += attachmentMapper.insertList(list);
				}
				result += naturalMapper.updateByPrimaryKeySelective(natural);
				if (result > 0) {
					return "自然环境修改成功";
				} else {
					return "自然环境修改失败";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RelicsException("系统错误，操作失败");
		}
	}
	public RelicsNaturalEnvironment selectObjByKey(Integer basicId)
			throws RelicsException {
		try {
			return naturalMapper.selectObjByKey(basicId);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}
	public PaginationVO selectList(RelicsNaturalEnvironment natural, PaginationVO pageVo)
			throws RelicsException {
		try {
			pageVo.setTotalRecord(naturalMapper.selectTotalRecord(natural));
			pageVo.setList(naturalMapper.selectList(natural, pageVo));
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
		return pageVo;
	}
	public int deleteByKey(List<?> keys) throws RelicsException {
		try {
			return naturalMapper.deleteByKey(keys);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}
	public RelicsNaturalEnvironment selectThisObjByKey(Integer mappingId)
			throws RelicsException {
		try {
			return naturalMapper.selectByPrimaryKey(mappingId);
		} catch (Exception e) {
			throw new RelicsException("系统异常");
		}
	}
}
